package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.VMS_STATUS_TOPIC)
@Slf4j
public class AutoCreateHandler implements MsgHandler {
    @Autowired
    private TaskService taskService;
    @Autowired
    private VMService vmService;

    @Override
    public void process(String jsonMsg) throws IOException {
        log.info("接收成功");
        VmStatusContract vmStatusContract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        if(vmStatusContract==null||vmStatusContract.getStatusInfo()==null||vmStatusContract.getStatusInfo().size()<=0)return;
        if(vmStatusContract.getStatusInfo().stream().anyMatch(s->s.isStatus()==false)){
            VmVO vmVO = vmService.getVMInfo(vmStatusContract.getInnerCode());
            if(vmVO==null){
                return;
            }
            int n = taskService.getLeastUser(vmVO.getRegionId(), true);
            if(n!=0){
                TaskViewModel taskViewModel = new TaskViewModel();
                taskViewModel.setUserId(n);
                taskViewModel.setInnerCode(vmStatusContract.getInnerCode());
                taskViewModel.setProductType(VMSystem.TASK_TYPE_REPAIR);
                taskViewModel.setCreateType(0);
                taskViewModel.setAssignorId(0);
                taskViewModel.setDesc(jsonMsg);
                taskService.createTask(taskViewModel);
            }
        }
    }
}
